package ru.lenwp.operation.client;

import ru.lenwp.context.Context;
import ru.lenwp.operation.Operation;
import ru.lenwp.operation.OperationService;
import ru.lenwp.operation.admin.*;
import ru.lenwp.util.ConsoleUtil;
import ru.lenwp.util.OperationUtil;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Lenar on 02.07.2014.
 */
public class ClientOperationService implements OperationService {

    private final Context context;

    private final Map<Integer, Operation> operationMap;

    public ClientOperationService(Context context) {
        this.context = context;
        operationMap = new LinkedHashMap<Integer, Operation>();
        operationMap.put(1, new CreateAccountOperation(context));
        operationMap.put(2, new RechangeAccount(context));
        operationMap.put(3, new TransferThisClient(context));
    }

    public void runService() {
        OperationUtil.showOperationList(operationMap);
        int operationNumber = 0;
        do {
            System.out.println("Выбирите операцию (0 - Выход):");
            operationNumber = ConsoleUtil.askMenuNumber();
            if (operationNumber != 0) {
                Operation operation = operationMap.get(operationNumber);
                if (operation != null) {
                    operation.execute();
                } else {
                    System.out.println("Неверно выбрана операция");
                }
                OperationUtil.showOperationList(operationMap);
            }
        } while (operationNumber != 0);
    }
}